package com.deanlib.cabinet.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;


import com.deanlib.cabinet.data.network.Response;
import com.elvishew.xlog.XLog;

import java.util.Optional;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public abstract class BaseFragment<V extends ViewDataBinding> extends Fragment/*RxFragment*/
        implements IUIAuxiliary, IPopupAuxiliary,INetworkAuxiliary,IActionProtocol {

    protected V binding;
    public View rootView;
    public LayoutInflater inflater;
    protected NetworkAuxiliary networkAuxiliary;
    private PopupAuxiliary popupAuxiliary;
    private CompositeDisposable compositeDisposable;

    public static String TAG = "BaseFragment";

    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        TAG = getClass().getSimpleName();
        this.inflater = inflater;
        if (rootView == null) {
            binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
//            rootView = inflater.inflate(this.getLayoutId(), container, false);
            rootView = binding.getRoot();
            popupAuxiliary = new PopupAuxiliary(binding, this);
            networkAuxiliary = new NetworkAuxiliary(popupAuxiliary);
            init(savedInstanceState);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        initData(savedInstanceState);
        initView();
        loadData();
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onDestroyView() {
        dispose();
        super.onDestroyView();
    }

    @Override
    public void toast(String msg) {
        if (popupAuxiliary !=null) {
            popupAuxiliary.toast(msg);
        }
    }

    @Override
    public void loading(String title, String content) {
        if (popupAuxiliary !=null) {
            popupAuxiliary.loading(title, content);
        }
    }

    @Override
    public void loaded() {
        if (popupAuxiliary !=null) {
            popupAuxiliary.loaded();
        }
    }

    @Override
    public void networkError() {
        if (popupAuxiliary !=null) {
            popupAuxiliary.networkError();
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next, error);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error, Action complete) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next, error, complete);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public void dispose() {
        if (networkAuxiliary !=null){
            networkAuxiliary.dispose();
        }
    }

    protected void addDisposable(Disposable disposable){
        if (compositeDisposable == null){
            synchronized (this){
                if (compositeDisposable == null){
                    compositeDisposable = new CompositeDisposable();
                }
            }
        }
        compositeDisposable.add(disposable);
    }

    @Override
    public void onDestroy() {
        dispose();
        if (compositeDisposable!=null) {
            compositeDisposable.dispose();
        }
        super.onDestroy();
    }
}
